package org.demo.controller;

import java.util.Locale;

import org.demo.entity.AppEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 登录和首页
 * @author garca
 *
 */
@Controller
public class LoginController {

	@Autowired
	private MessageSource messageSource;
	
	@Autowired
	private AppEnvironment appEnvironment;

	/**
	 * 默认页面
	 * 
	 * @return
	 */
	@RequestMapping("/")
	public String root() {
		return "redirect:/index";
	}

	/**
	 * 首页
	 * 
	 * @param model
	 * @param locale
	 * @return
	 */
	@RequestMapping("/index")
	public String index(Model model, Locale locale) {
		model.addAttribute("title", messageSource.getMessage("text.title", null, locale));
		model.addAttribute("environment", appEnvironment);
		return "index";
	}

	/**
	 * 登录页面
	 * 
	 * @return
	 */
	@RequestMapping("/login")
	public String login() {
		return "login";
	}

	/**
	 * 登录失败
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/login-error")
	public String loginError(Model model) {
		model.addAttribute("loginError", true);
		return "login";
	}

	/**
	 * 无权限,其实是403
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/no-right")
	public String noRight(Model model) {
		model.addAttribute("loginError", true);
		return "401";
	}
}
